// *** LICENSE HEADER ***
// Filename: ./packetdata.h
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#ifndef PACKETDATA_H
#define PACKETDATA_H

#include "helperfunctions.h"
#include "definitions.h"
#include <cstdio>
#include <SDL.h>
#include <SDL_net.h>

#define SEREC_ACK_WAIT_DELAY 500
#define SEREC_RETRY_NUMBER 3

//Class holding the packet data for a single UDP packet.
class packetData{
    public:
        //Constructors
        packetData(UDPpacket* thePacket = NULL);
        packetData(int packetSize);
        ~packetData();

        //Copy constructor
        packetData(packetData const& other);

        //Assignment operator
        packetData& operator=(packetData const& other);

        //Set the number of the packet
        void SetPacketNumber(Uint32 num);
        //Set the packet type
        void SetPacketType(bool isData);
        //Copy the message into the packet
        void SetMessage(const char* message);
        //Set the packet destination address
        void SetAddress(IPaddress addr);

        //Check the packet type on data packet
        bool IsData();
        //Get the packet number
        Uint32 GetPacketNumber();
        //Get the packet address
        IPaddress GetAddress();

        //Get the packet pointer
        UDPpacket* GetPacket();
        //Free the packet
        void FreePacket();

        //Update the packet wait time
        void Update(int dt);
        //Get the packet wait time
        int GetWaitTime();
        //Try to send the packet anew
        void NewTry();
        //Check, whether the packet has any more retries left to be sent
        bool HasRetriesLeft();
    private:
        UDPpacket* packet;

        //Number of retries left
        int retriesLeft;
        //Wait time for the packet before another try to be sent
        int waitTime;
};

#endif
